// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/connectors/hive/storage_adapters/abfs/tests/MockDataLakeFileClient.h>

#include <filesystem>

#include <azure/storage/files/datalake.hpp>

namespace kumo::pollux::filesystems {

void MockDataLakeFileClient::create() {
  fileStream_ = std::ofstream(
      filePath_,
      std::ios_base::out | std::ios_base::binary | std::ios_base::app);
}

PathProperties MockDataLakeFileClient::getProperties() {
  if (!std::filesystem::exists(filePath_)) {
    Azure::Storage::StorageException exp(filePath_ + "doesn't exists");
    exp.StatusCode = Azure::Core::Http::HttpStatusCode::NotFound;
    throw exp;
  }
  std::ifstream file(filePath_, std::ios::binary | std::ios::ate);
  uint64_t size = static_cast<uint64_t>(file.tellg());
  PathProperties ret;
  ret.FileSize = size;
  return ret;
}

void MockDataLakeFileClient::append(
    const uint8_t* buffer,
    size_t size,
    uint64_t offset) {
  fileStream_.seekp(offset);
  fileStream_.write(reinterpret_cast<const char*>(buffer), size);
}

void MockDataLakeFileClient::flush(uint64_t position) {
  fileStream_.flush();
}

void MockDataLakeFileClient::close() {
  fileStream_.flush();
  fileStream_.close();
}
} // namespace kumo::pollux::filesystems
